package com.situ.gym.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.gym.mapper.UserRoleMapper;
import com.situ.gym.mapper.RoleMapper;
import com.situ.gym.mapper.UserRoleMapper;
import com.situ.gym.pojo.*;
import com.situ.gym.pojo.User;
import com.situ.gym.pojo.User;
import com.situ.gym.mapper.UserMapper;
import com.situ.gym.pojo.query.UserQuery;
import com.situ.gym.pojo.vo.ChartCountVO;
import com.situ.gym.pojo.vo.PermissionVO;
import com.situ.gym.pojo.vo.WeekDayCountVO;
import com.situ.gym.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-08-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PermissionServiceImpl permissionServiceImpl;

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<WeekDayCountVO> selectUserCount(Date date) {
        return userMapper.selectUserCount(date);
    }
    @Override
    public IPage<User> list(UserQuery userQuery) {
        IPage<User> page  = new Page<>(userQuery.getPage(), userQuery.getLimit());
        return userMapper.list(page, userQuery);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        userMapper.deleteAll(ids);
    }

    @Override
    public List<ChartCountVO> selectUserCount() {
        return List.of();
    }


    @Override
    public User selectByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    public Map<String, Object> selectUserPermissionInfoById(Integer id) {
        //根据id查找用户信息
        User user = userMapper.selectById(id);
        //根据user的id查找菜单（路由）权限
        List<PermissionVO> routerVOList = permissionServiceImpl.selectRouterTreeByUserId(id);
        //根据user的id查找按钮权限
        List<String> btnList = permissionServiceImpl.selectBtnListByUserId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("user",user);
        map.put("routers",routerVOList);
        map.put("btnList",btnList);
        return map;
    }

    @Override
    public Map<String, Object> selectAssignRole(Integer userId) {
        List<Role> roleList = roleMapper.selectList(null);
        List<Long> assignedRoleIdList = userRoleMapper.selectByUserId(userId);

        Map<String, Object> map = new HashMap<>();
        map.put("roleList", roleList);
        map.put("assignedRoleIdList", assignedRoleIdList);
        return map;
    }

    @Override
    public void assignRole(Long userId, Long[] roleIds) {
        //在user_role表里面先删除userId已经分配的角色
        userRoleMapper.deleteByUserId(userId);
        //给这个用户重新分配角色
        for (Long roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleMapper.insert(userRole);
        }

    }
}
